package com.wx.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.admin.service.SysDictService;
import com.wx.common.utils.StringUtils;
import com.wx.common.web.BaseController;
import com.wx.pojo.SysDict;
import com.wx.pojo.SysDictExample;
import oracle.jdbc.driver.DatabaseError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import sun.misc.Request;;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "${adminPath}/sys-dict")
public class SysDictController  extends BaseController {

    @Autowired
    private SysDictService sysDictService;

    @RequestMapping(value = "list")
    public String list(Model model,@RequestParam(required = false) String keyword, @RequestParam(required = false)Date startTime,@RequestParam(required = false) Date endTime, @RequestParam(required = true,defaultValue = "1") int pageIndex, @RequestParam(required = true,defaultValue = "10") int pageSize)
    {
        PageInfo<SysDict> sysDictList =sysDictService.getPageList(keyword,startTime,endTime,pageIndex,pageSize);
        model.addAttribute("sysDictList",sysDictList.getList());
        model.addAttribute("totalCount",sysDictList.getTotal());

        return "sysDict/list";
    }

    @RequestMapping(value = "form")
    public String form(SysDict sysDict, Model model)
    {
        sysDict = sysDictService.getById(sysDict.getId());
        sysDict = sysDict==null ? new SysDict(): sysDict;
        model.addAttribute("sysDict",sysDict);
        return "sysDict/form";
    }


    @RequestMapping(value = "save")
    @ResponseBody
    public String save(SysDict sysDict, HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes)
    {

        if(sysDict.getId()==null)
        {
            sysDictService.insert(sysDict.getKeyword(),sysDict.getValue(),sysDict.getRemarks(),sysDict.getDelFlag());
        }else{
            SysDict dict = sysDictService.getById(sysDict.getId());
            dict.setValue(sysDict.getValue());
            dict.setKeyword(sysDict.getKeyword());
            dict.setRemarks(sysDict.getRemarks());
            dict.setDelFlag(sysDict.getDelFlag());
            sysDictService.update(dict);
        }
        return "0";
        //return "redirect:"+ adminPath+ "/sys-dict/form";
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(long id)
    {
        sysDictService.delete(id);
        return "0";
    }

}
